

#include "unity.h"
#include "unity_fixture.h"
#include <string.h>
#include <stddef.h>
#include "log2.h"
#include "BSP.h"
#include "BSP_Sleep.h"
#include <Utility/BitReverse.h>
#include "rfid_common.h"
#include "HIDProx.h"
#include "id_card2.h"

#include "FreeRTOS.h"
#include "task.h"
#include "stream_buffer.h"

#include "test_platform.h"

#include "SystemView/SEGGER_RTT.h"

static StreamBufferHandle_t hStreamBuffer;

void rf_data_input_isr(uint16_t *data, uint16_t len)
{
	BaseType_t xHigherPriorityTaskWoken = pdFALSE;
	
	size_t ret = xStreamBufferSendFromISR(hStreamBuffer, data, len*2, &xHigherPriorityTaskWoken);
	if(ret != len*2){
		LOG2_ERR("send error: %d", ret);
	}
	portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}


TEST_GROUP(LF);


TEST_SETUP(LF)
{
	//BSP_RFID_Enable();
}

TEST_TEAR_DOWN(LF)
{
	
}

int id_card_get(uint8_t uid[5]);

// TEST(LF, ReadUID)
// {
// 	int cnt = 0;
// 	int ret;
// 	HidProxTag_T tag;
// 	hStreamBuffer = xStreamBufferCreate(1024*2, 1);
// 	HIDProx_Init();
// 	BSP_RFID_Enable();
// 	while(1){
// 		uint32_t t1,t2;
// 		// xStreamBufferReset(hStreamBuffer);
// 		id_card_reset();
// 		// BSP_RFID_Enable();
// 		t1 = xTaskGetTickCount();
// 		while(1){
// 			uint16_t data[128];
// 			size_t rlen = xStreamBufferReceive(hStreamBuffer, data, sizeof(data), 100);

// 			id_card_data_decode(data, rlen/2);
// 			ret = id_card_get(tag.uid);
// 			if(ret == 0){			
// 				// BSP_RFID_Disable();
// 				LOG2_INF("id: %02x%02x%02x%02x%02x", tag.uid[0], tag.uid[1], tag.uid[2], tag.uid[3], tag.uid[4]);
// 				// xStreamBufferReset(hStreamBuffer);
// 				// BSP_Sleep_Ms(200);
// 				// id_card_reset();
// 				// BSP_RFID_Enable();
// 				break;
// 			}
// 			t2 = xTaskGetTickCount();
// 			if((t2 - t1) > 200U){
// 				// BSP_RFID_Disable();
// 				LOG2_ERR("Read UID timeout");
// 				// xStreamBufferReset(hStreamBuffer);
// 				break;
// 			}
// 		}

// 	}
// }

// TEST(LF, ReadUID2)
// {
// 	int cnt = 0;
// 	int ret;
// 	HidProxTag_T tag;
// 	hStreamBuffer = xStreamBufferCreate(1024*2, 1);
// 	HIDProx_Init();
// 	BSP_RFID_Enable();
// 	while(1){
// 		uint32_t t1,t2;		
// 		id_card_reset();
		
// 		while(1){
// 			uint16_t data[128];
// 			size_t rlen = xStreamBufferReceive(hStreamBuffer, data, sizeof(data), 100);

// 			id_card_data_decode(data, rlen/2);
// 			ret = id_card_get(tag.uid);
// 			if(ret == 0){
// 				LOG2_INF("id: %02x%02x%02x%02x%02x", tag.uid[0], tag.uid[1], tag.uid[2], tag.uid[3], tag.uid[4]);
// 				break;
// 			}
// 		}
// 	}
// }

static uint8_t jscope_buf[2048];
void id_card_level_buf_clear(void);
void bsp_lf_adc_phase(uint16_t pulse);
uint32_t bsp_lf_rf_period(void);
static void lf_setup(void){
	BSP_RFID_Disable();
	uint8_t buf[128] = {0};
	//输入adc触发点
	LOG2_RESET_LEVEL();
	LOG2_INF("period: %u, input adc trigger...", bsp_lf_rf_period());
	int n = test_io_wait_line((char*)buf, sizeof(buf));
	LOG2_INF("adc trigger: %s", buf);
	int adc = atoi((char*)buf);
	BSP_RFID_Enable();
	bsp_lf_adc_phase(adc);
}
void id_card_print_level_buf(void);
int id_card_data_decode2(uint16_t *data, uint16_t len);
int id_card_data_decode3(uint16_t *data, uint16_t len);
#if 1
TEST(LF, ReadUID3)
{
	int cnt = 0;
	int ret;
	HidProxTag_T tag;
	uint8_t mode = 0;
	//初始化RTT
	SEGGER_RTT_ConfigUpBuffer(1, "JScope_u2", jscope_buf, 2048, SEGGER_RTT_MODE_NO_BLOCK_SKIP);

	hStreamBuffer = xStreamBufferCreate(1024*2, 1);
	HIDProx_Init();
	
	while(1){
		uint32_t t1,t2;
		uint32_t cnt = 0;
		id_card_reset();
		lf_setup();
		while(test_io_getc() < 0){
			static uint16_t data[256];
			size_t rlen = xStreamBufferReceive(hStreamBuffer, data, sizeof(data), 100);
			SEGGER_RTT_Write(1, data, rlen);
			if(mode == 0){
				ret = id_card_data_decode3(data, rlen/2);
				if(ret){
					if(ret > 0){
						LOG2_INF("id_card_data_decode3 ok");
					}
					else{
						LOG2_ERR("id_card_data_decode3 error");
					}
					//id_card_level_buf_clear();
					id_card_print_level_buf();
					// data[0] = 0;
					// SEGGER_RTT_Write(1, data, 2);
				}
			}
			else{
				ret = id_card_data_decode2(data, rlen/2);
				if(ret){
					if(ret > 0){
						LOG2_INF("id_card_data_decode2 success");
					}
					else{
						LOG2_ERR("id_card_data_decode2 error");
					}
					
					id_card_print_level_buf();
				}				
			}
			

			ret = id_card_get(tag.uid);
			if(ret == 0){
				LOG2_INF("id: %02x%02x%02x%02x%02x", tag.uid[0], tag.uid[1], tag.uid[2], tag.uid[3], tag.uid[4]);
				// id_card_print_level_buf();
				// id_card_reset();
				cnt = 0;
				// break;
			}
			else{
				if(cnt > 128){
					// LOG2_ERR("timeout");
					// id_card_print_level_buf();
					cnt = 0;
				}
				else{
					cnt++;
				}
			}
		}
	}
}
#endif
//模拟数据解析
static const char sim_data[] = "1010101010101001010101011010101001010101010101010101011001101010011010101010011001010101100110010101101001100101101010100101101010101010101010010101010110101010010101010101010101010110011010100110101010100110010101011001100101011010011001011010101001011";
// static const char sim_data[] = "110101010101010101001010101011010101001010101010101010101011001101010011010101010011001010101100110010101101001100101101010100101";
static uint32_t id_card_data_sim(uint32_t offset, uint8_t num, uint16_t* adc_data){
	
	uint32_t adc_num = 0;
	for(uint32_t i = offset; i < offset + num; i++, adc_num += 32){
		if(sim_data[i] == '0'){
			for(int j = 0; j < 32; j++){
				adc_data[adc_num + j] = 0x800;
			}
		}
		else{			
			for(int j = 0; j < 32; j++){
				adc_data[adc_num + j] = 0x400;
			}
		}
	}
	return adc_num;
}
#if 0
TEST(LF, ReadUID4)
{
	int cnt = 0;
	int ret;
	int input;
	uint8_t mode = '1';
	HidProxTag_T tag;
	uint32_t rlen;
	uint32_t off;
	//初始化RTT
	SEGGER_RTT_ConfigUpBuffer(1, "JScope_u2", jscope_buf, 2048, SEGGER_RTT_MODE_NO_BLOCK_SKIP);

	hStreamBuffer = xStreamBufferCreate(1024*2, 1);
	HIDProx_Init();
	
	id_card_reset();
	// lf_setup();	
	while(1){

		static uint16_t data[256];
		if(off >= sizeof(sim_data)-1){
			off = 0;
		}
		rlen = id_card_data_sim(off, 4, data);
		off += 4;

		SEGGER_RTT_Write(1, data, rlen*2);

		input = test_io_getc();
		if(input > 0){
			mode = input;
		}

		if(mode == '1'){
			ret = id_card_data_decode3(data, rlen);
		}
		else{
			ret = id_card_data_decode2(data, rlen);
		}
		
		if(ret){
			if(ret > 0){
				LOG2_INF("id_card_data_decode2 success");
			}
			else{
				LOG2_ERR("id_card_data_decode2 error");
			}
			
			id_card_print_level_buf();
		}
		ret = id_card_get(tag.uid);
		if(ret == 0){
			LOG2_INF("id: %02x%02x%02x%02x%02x", tag.uid[0], tag.uid[1], tag.uid[2], tag.uid[3], tag.uid[4]);
			// id_card_print_level_buf();
			id_card_reset();
			BSP_Sleep_Ms(1000);
			// break;
		}
	}
}
#endif


TEST(LF, ReadHIDProx)
{
	int cnt = 0;
	int ret;
	HidProxTag_T tag;
	uint8_t mode = 0;
	//初始化RTT
	SEGGER_RTT_ConfigUpBuffer(1, "JScope_u2", jscope_buf, 2048, SEGGER_RTT_MODE_NO_BLOCK_SKIP);

	hStreamBuffer = xStreamBufferCreate(1024*2, 1);
	HIDProx_Init();
	
	while(1){
		uint32_t t1,t2;
		uint32_t cnt = 0;
		id_card_reset();
		lf_setup();
		while(test_io_getc() < 0){
			static uint16_t data[256];
			size_t rlen = xStreamBufferReceive(hStreamBuffer, data, sizeof(data), 100);
			SEGGER_RTT_Write(1, data, rlen);
			HIDProxDecoder_Input(data, rlen/2);
			ret = HIDProx_Read(&tag);
			
			if(ret == 0){
				LOG2_INF("id: %02x%02x%02x%02x%02x", tag.uid[0], tag.uid[1], tag.uid[2], tag.uid[3], tag.uid[4]);
				// id_card_print_level_buf();
				// id_card_reset();
				cnt = 0;
				// break;
			}
			else{
				if(cnt > 128){
					// LOG2_ERR("timeout");
					// id_card_print_level_buf();
					cnt = 0;
				}
				else{
					cnt++;
				}
			}
		}
	}
}